package tcln.gxtx.client.widget.treegrid.utils;

import tcln.gxtx.client.utils.lang.GwtNumberUtils;

import com.extjs.gxt.ui.client.data.ModelData;

/**
 * Default implementation of a {@link TreeGridModelDataFormatter} which handles basic formatting.
 * 
 * @param <M>
 *            Model data.
 * @author Truong Chau Lien Ngoc (lienngoc@gmail.com)
 */
public class DefaultTreeGridModelDataFormatter<M extends ModelData> implements TreeGridModelDataFormatter<M> {

    /**
     * {@inheritDoc}
     */
    @Override
    public String format(M model, String property) {
        Object value = model.get(property);
        return formatValue(value);
    }

    /**
     * Formats given model value. This method is used for the case when only value is known.
     * 
     * @param value
     * @return
     */
    protected String formatValue(Object value) {

        // null --> don't display anything
        if (value == null) {
            return "";
        }

        // process value and format it
        if (value instanceof Double) {
            return GwtNumberUtils.formatDecimal((Double) value);
        } else if (value instanceof Float) {
            return GwtNumberUtils.formatDecimal((Float) value);
        } else if (value instanceof Integer) {
            return GwtNumberUtils.formatInteger((Integer) value);
        } else {
            return String.valueOf(value);
        }
    }
}
